﻿utils.setInterval(FetchAndParse, 1000 * 60);

function view_onOpen() {
 FetchAndParse();
}

//Fetches the Rss feed from the selected site.
function FetchAndParse() {
  var url = "http://www.ndtv.com/cricketcard/aspfiles/coke.asp";  
  httpReq = new XMLHttpRequest();
  httpReq.onreadystatechange = OnRssFeed;
  httpReq.open("GET", url, true);
  httpReq.send();  
}
var innings, t1, t1_runs, t1_overs, t1_wickets , t2, t2_runs, t2_overs, t2_wikets;
var venue, toss, tossdec, dated, matchstatus, target, session;
var bName, bRuns, bBalls, bFours, bSix, bOvers, bMaidens, bWickets, bOutstatus, bCb, bWides, bNoballs;
function OnRssFeed() {
   if(httpReq.readyState==4){
      if(httpReq.status==200){
         var doc = new DOMDocument();
         doc.loadXML(httpReq.responseText);
         var cricItems = doc.getElementsByTagName("cricket");
	     
         if(cricItems.length>0) {
					  for(var item = cricItems[0].firstChild; item != null; item = item.nextSibling){
							if(item.nodeName=="scorecard"){
								for (var node = item.firstChild; node != null; node = node.nextSibling) {
										 if (node.nodeName == "currentinnings")
												innings = node.firstChild.nodeValue;
										 if(node.nodeName == "teamA"){
												t1 = getFirst3(node.firstChild.nodeValue);
												t1_runs = node.getAttribute("runs");
												t1_overs = node.getAttribute("overs");
												t1_wickets = node.getAttribute("wickets");
											}
										 if(node.nodeName == "teamB"){
												t2 = getFirst3(node.firstChild.nodeValue);
												t2_runs = node.getAttribute("runs");
												t2_overs = node.getAttribute("overs");
												t2_wickets = node.getAttribute("wickets");
											}
											if(node.nodeName == "otherdetail") {
												venue = node.getAttribute("venue");
												toss = node.getAttribute("tosswonby");
												tossdec = node.getAttribute("tossdec");
												dated = node.getAttribute("dated");
												matchstatus = node.getAttribute("matchstatus");
												session = node.getAttribute("session");
												TossWon.innerText = "Toss:" + toss;
												target = node.getAttribute("target");
											}
								}
							}else
							if(item.nodeName=="batting"){ var bcnt =0;
								//Batting
								for (var node = item.firstChild; node != null; node = node.nextSibling) {
										 if(node.nodeName == "batsman"){
												bName = node.getAttribute("name");
												bRuns = node.getAttribute("runs");
												bBalls = node.getAttribute("balls");
												bFours = node.getAttribute("four");
												bSix = node.getAttribute("six");
												bOutstatus = node.getAttribute("outstatus");
												bCb = node.getAttribute("cb");
											}
											if(bcnt == 0){
												Bat_1.innerText = bName;// + (bCb=="1"?"*":"");
												Runs_1.innerText = bRuns;
												Balls_1.innerText = bBalls;
												Fours_1.innerText = bFours;
												Sixs_1.innerText = bSix;
											}else
											if(bcnt == 1){
												Bat_2.innerText = bName;// + (bCb=="1"?"*":"");
												Runs_2.innerText = bRuns;
												Balls_2.innerText = bBalls;
												Fours_2.innerText = bFours;
												Sixs_2.innerText = bSix;
											}
											bcnt ++;
								}
							}else
							if(item.nodeName=="bowling"){ var bcnt =0;
								//Bowling
								for (var node = item.firstChild; node != null; node = node.nextSibling) {
										 if(node.nodeName == "bowler"){
												bName = node.getAttribute("name");
												bRuns = node.getAttribute("runs");
												bOvers = node.getAttribute("overs");
												bMaidens = node.getAttribute("maidens");
												bWickets = node.getAttribute("wickets");
												bWides = node.getAttribute("wides");
												bNoballs = node.getAttribute("noballs");
												bCb = node.getAttribute("cb");
											}
											if(bcnt == 0){
												Bow_1.innerText = bName;// + (bCb=="1"?"*":"");
												bRuns_1.innerText = bRuns;
												Overs_1.innerText = bOvers;
												Maidens_1.innerText = bMaidens;
												Wickets_1.innerText = bWickets;
												
											}else
											if(bcnt == 1){
												Bow_2.innerText = bName;// + (bCb=="1"?"*":"");
												bRuns_2.innerText = bRuns;
												Overs_2.innerText = bOvers;
												Maidens_2.innerText = bMaidens;
												Wickets_2.innerText = bWickets;
											}
											bcnt ++;
									}
							}
						}
         }
				if(innings=="First" && !(t1!=undefined && t2!=undefined)){
					TeamA.innerText = t1 + ":" + t1_runs + "/" + t1_wickets + " " + t1_overs + target;
				}else{
					TeamA.innerText = t1 + ":" + t1_runs + "/" + t1_wickets + " " + t1_overs + target;
					TeamB.innerText = t2 + ":" + t2_runs + "/" + t2_wickets + " " + t2_overs;
				}
				caption = "Cricket Live - " + t1 + " vs " + t2;
				if(target!=undefined && target!="")
					TeamA.innerText = TeamA.innerText + " ("+target +")";
        matchstatus = matchstatus + " " + dated+ " at " +venue ;
				MatchStatus.innerText = matchstatus;
      }
   }
}

function getFirst3(teamName){
  var team = new String(teamName);
  team = team.toUpperCase();
  if(team.length >= 3)
    return team.substring(0,3);
  else
		return team;
}

function onMini() {
 caption = "Cricket Live - " + t1 + " - " + t1_runs + "/" + t1_wickets;
}

function onRestore() {
caption = "Cricket Live - " + t1 + " vs " + t2;
}
